﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using CommonLibrary.DataModel.Basic;
using CommonLibrary.Common;
using CommonLibrary.Wf.Common;
using CommonLibrary.Wf.InterFace;
using ServiceApplication.Service.Basic;


namespace ServiceApplication.Service.WorkFlow.WorkflowDesignerApp
{
    public partial class FrmChoiceYh : Form
    {
        /**
         * 结果
         * result[0]表示按人员(0)还是按职务(1),(2)表示置空
         * result[1]表示人员编码或者职务编码
         * result[2]表示人员名称或者职务[]名称
         * result[3]表示选择按职务的时候，是否按部门(0表示是，1表示否)
         * */
        public ChoiceClrxx result;

        BasicService bs = new BasicService();
        WorkFlowService wfs = new WorkFlowService();

        #region 构造

        public FrmChoiceYh(ChoiceClrxx clrxx)
        {
            InitializeComponent();

            result = clrxx;
            IntityForm();
            showInitInfo(result);
        }

        private void IntityForm()
        {
            //人员tree：   将子node写入treeview
            treeView_ry_all.Nodes.AddRange(bs.GetUserNodeList());
            treeView_ry_all.CollapseAll();
            //职务选择列
            var dt = wfs.WfSpqxQueryAll();
            checkedListBoxAqx.Items.AddRange(dt.AsEnumerable().Select(s => s.Field<string>("名称")).ToArray());

        }

        private void showInitInfo(ChoiceClrxx clrxx)
        {
            //显示初始信息,如果有值显示选择好的信息
            switch (clrxx.处理者类型)
            {
                case "按人员":
                    radioButtonAry.Checked = true;
                    for (int i = 0; i < clrxx.处理人编码.Length; i++)
                    {
                        UserEntity userEntity = bs.QueryUser(clrxx.处理人编码[i]);
                        if (userEntity == null) continue;
                        ListViewItem item = new ListViewItem(userEntity.user_name);
                        item.Tag = userEntity.user_id;
                        this.listView_rys.Items.Add(item);
                    }
                    //listView1.Items.Add(new ListViewItem(
                    break;
                case "发起人":
                    radioButtonFqr.Checked = true;
                    break;
                case "按权限":
                    radioButtonAzw.Checked = true;
                    checkedListBoxAqx.Enabled = true;
                    for (int i = 0; i < checkedListBoxAqx.Items.Count; i++)
                    {
                        if (clrxx.按权限.Contains(checkedListBoxAqx.Items[i].ToString()))
                        {
                            checkedListBoxAqx.SetItemChecked(i, true);
                        }
                    }

                    CB_bbm.Checked = clrxx.本部门;
                    CB_bbm.Enabled = true;
                    break;
                case "由上一步处理人指定":
                    radioButtonZdSyb.Checked = true;
                    break;

            }
        }


        #endregion

        #region 界面事件

        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonAry.Checked)
            {
                groupBoxRy.Enabled = true;
                checkedListBoxAqx.Enabled = false;

                CB_bbm.Enabled = false;
            }
            else if (radioButtonFqr.Checked)
            {
                groupBoxRy.Enabled = false;
                checkedListBoxAqx.Enabled = false;
                CB_bbm.Enabled = false;
            }
            else if (radioButtonZdSyb.Checked)
            {
                groupBoxRy.Enabled = false;
                checkedListBoxAqx.Enabled = false;
                CB_bbm.Enabled = false;
            }
            else if (radioButtonAzw.Checked)
            {
                groupBoxRy.Enabled = false;
                checkedListBoxAqx.Enabled = true;
                CB_bbm.Enabled = true;
            }
        }

        #endregion

        #region 工具栏

        //确定
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            //根据checkbox判断获得clrs
            //人员
            if (radioButtonAry.Checked)
            {
                if (listView_rys.Items.Count == 0)
                {
                    MessageBox.Show("请选择人员");
                    return;
                }
                int[] clrcode = new int[listView_rys.Items.Count];
                for (int i = 0; i < listView_rys.Items.Count; i++)
                {
                    clrcode[i] = int.Parse(listView_rys.Items[i].Tag.ToString());
                }
                result.处理者类型 = TaskClrType.按人员.ToString();
                result.处理人编码 = clrcode;
                result.处理者名称 = ""; //? mlh
            }
            else if (radioButtonFqr.Checked)
            {
                result = new ChoiceClrxx();
                result.处理者类型 = TaskClrType.发起人.ToString();
            }
            else if (radioButtonZdSyb.Checked)
            {
                result = new ChoiceClrxx();
                result.处理者类型 = TaskClrType.由上一步处理人指定.ToString();
            }
            else if (radioButtonAzw.Checked)
            {

                List<string> tmp = new List<string>();
                for (int i = 0; i < checkedListBoxAqx.Items.Count; i++)
                {
                    if (checkedListBoxAqx.GetItemChecked(i))
                        tmp.Add(checkedListBoxAqx.Items[i].ToString());
                }
                if (tmp.Count == 0)
                {
                    MessageBox.Show("请选择权限");
                    return;
                }
                result = new ChoiceClrxx();
                result.处理者类型 = TaskClrType.按权限.ToString();
                result.按权限 = tmp.ToArray();
                result.本部门 = CB_bbm.Checked;
            }
            else
            {
                MessageBox.Show("请确定一种方式");
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        //关闭
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        #endregion

        private void button_allright_Click(object sender, EventArgs e)
        {
            TreeNode tn = treeView_ry_all.SelectedNode;
            if (tn == null) return;
            NodeTag nt = tn.Tag as NodeTag;

            //选中的是叶节点
            if (nt.level == 1)
            {
                //是否已被选择
                foreach (ListViewItem li in listView_rys.Items)
                {
                    if (li.Text == nt.Text) return;
                }
                ListViewItem lvi = new ListViewItem(tn.Text);
                lvi.Tag = nt.Value;
                listView_rys.Items.Add(lvi);
            }

            //选中的是次叶节点
            if (nt.level == 2)
            {
                NodeTag childNt;
                ListViewItem lvi;
                bool isExist;
                foreach (TreeNode item in tn.Nodes)
                {
                    isExist = false;
                    //是否已被选择
                    foreach (ListViewItem li in listView_rys.Items)
                    {
                        if (li.Text == item.Text)
                            isExist = true;
                    }
                    if (!isExist)
                    {
                        childNt = item.Tag as NodeTag;
                        lvi = new ListViewItem(item.Text);
                        lvi.Tag = nt.Value;
                        listView_rys.Items.Add(lvi);
                    }
                }
            }
        }

        private void button_right_Click(object sender, EventArgs e)
        {
            TreeNode tn = treeView_ry_all.SelectedNode;
            if (tn == null) return;

            //是否已被选择
            foreach (ListViewItem item in listView_rys.Items)
            {
                if (item.Text == tn.Text) return;
            }

            NodeTag nt = tn.Tag as NodeTag;

            if (nt.level == 1)
            {
                ListViewItem lvi = new ListViewItem(tn.Text);
                lvi.Tag = nt.Value;
                listView_rys.Items.Add(lvi);
            }
        }

        private void button_left_Click(object sender, EventArgs e)
        {
            if (listView_rys.SelectedItems.Count == 0) return;

            listView_rys.Items.Remove(listView_rys.SelectedItems[0]);
        }

        private void button_allleft_Click(object sender, EventArgs e)
        {
            listView_rys.Items.Clear();
        }

        private void toolStripButton2_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
